package com.init.engineering.controller;

import com.init.engineering.pages.PageResult;
import com.init.engineering.pages.QueryPageBean;
import com.init.engineering.pages.Result;
import com.init.engineering.pojo.Branch;
import com.init.engineering.service.BranchService;
import com.init.engineering.utils.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/branch")
public class BranchController {
    @Autowired
    private BranchService branchService;


    @PostMapping("/all")
    public PageResult findBrachesByPages(@RequestBody QueryPageBean queryPageBean){
        System.out.println("进入BranchController");
        PageResult stagesByPage = branchService.findBranchesByPage(queryPageBean);
        return stagesByPage;
    }


    @PostMapping("/add")
    public Result addBranch(@RequestBody Branch branch){

        //随机ID
        branch.setBid(UUIDUtil.GetID());
        //获取系统更新时间
        branch.setUpdatetime(new Date());
        int row = branchService.saveBranch(branch);
        Result result = new Result();
        if(row > 0){
            result.setMessage("添加成功");
            result.setFlag(true);
        }else{
            result.setFlag(false);
            result.setMessage("添加失败，请重试");
        }
        return result;
    }


    @GetMapping("/delete/{bid}")
    public Result deleteBranch(@PathVariable("bid") String bid){
        int row = branchService.deleteBranch(bid);
        Result result = new Result();
        if(row > 0){
            result.setMessage("删除成功");
            result.setFlag(true);
        }else{
            result.setFlag(false);
            result.setMessage("删除失败，请重试");
        }
        return result;
    }


    @GetMapping("/findBranchByBid")
    public Result findStageBySid(String bid){
        Branch branch = branchService.findBranchByBid(bid);
        Result result = new Result();
        if (branch != null){
            result.setFlag(true);
            result.setMessage("回显成功");
            result.setData(branch);
        }else{
            result.setFlag(false);
            result.setMessage("数据回显失败");
        }
        return result;
    }


    @PostMapping("/update")
    public Result updateStage(@RequestBody Branch branch){
        branch.setUpdatetime(new Date());
        int row = branchService.updateBranch(branch);
        Result result = new Result();
        if(row > 0){
            result.setMessage("更新成功");
            result.setFlag(true);
        }else{
            result.setFlag(false);
            result.setMessage("更新失败，请重试");
        }
        return result;
    }

    @RequestMapping("/findBranches")
    public Result findBranches(){
        List<Branch> branches = branchService.findBranches();
        Result result = new Result();
        if(branches != null){
            result.setData(branches);
            result.setFlag(true);
        }else{
            result.setFlag(false);
        }
        return result;
    }
}
